//
//  HiSliderContainer.swift
//  
//
//  Created by Hoa on 2021/6/17.
//

import Foundation
import UIKit

var window: UIWindow!

open class HiSlider {
    
    var container: HiSliderContainer!
    
    public init(childViewController: UIViewController, childWidth: CGFloat = 300) {
        container = HiSliderContainer(childViewController: childViewController, childWidth: childWidth)
        container.delegate = self
    }
    
    @objc public func hidden() {
        container.hidden {
            window?.isHidden = true
            UIApplication.shared.windows.first?.makeKey()
        }
    }
    
    public func show() {
        window = UIWindow(frame: UIScreen.main.bounds)
        window.backgroundColor = .clear
        window.windowLevel = .alert + 2
        window.rootViewController = container
        window?.isHidden = false
        window?.makeKeyAndVisible()
        container.show()
    }
}
extension HiSlider: HiSliderContainerDelegate {
    
    func sliderClickedPlace(_ slider: HiSliderContainer) {
        hidden()
    }
}

protocol HiSliderContainerDelegate: AnyObject {
    func sliderClickedPlace(_ slider: HiSliderContainer)
}

open class HiSliderContainer: UIViewController {
    
    weak var delegate: HiSliderContainerDelegate!
    
    private var childViewController: UIViewController!
    private var childWidth: CGFloat = 0
    
    private let bgColor: UIColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
    
    /// 遮罩
    lazy var placeView: UIControl = {
        let placeView = UIControl()
        placeView.backgroundColor = bgColor
        placeView.addTarget(self, action: #selector(placeClick(_:)), for: .touchUpInside)
        return placeView
    }()
    
    @objc func placeClick(_ sender: UIControl) {
        delegate?.sliderClickedPlace(self)
    }
    
    init(childViewController: UIViewController, childWidth: CGFloat) {
        super.init(nibName: nil, bundle: nil)
        self.childViewController = childViewController
        self.childWidth = childWidth
    }
    
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    func setupUI() {
        
        view.backgroundColor = bgColor
        view.addSubview(placeView)
        let placeViewWidth = UIScreen.main.bounds.width - childWidth
        placeView.frame = CGRect(x: 0, y: 0, width: placeViewWidth, height: UIScreen.main.bounds.height)
        
        view.addSubview(childViewController.view)
        childViewController.view.frame = CGRect(x: UIScreen.main.bounds.width, y: 0, width: childWidth, height: UIScreen.main.bounds.height)
        addChild(childViewController)
        childViewController.didMove(toParent: self)
    }
    
    func show() {
        let placeViewWidth = UIScreen.main.bounds.width - childWidth
        UIView.animate(withDuration: 0.3) {
            self.childViewController.view.frame = CGRect(x: placeViewWidth, y: 0, width: self.childWidth, height: UIScreen.main.bounds.height)
        }
    }
    
    func hidden(_ completion: (() -> Void)?) {
        UIView.animate(withDuration: 0.3) {
            self.childViewController.view.frame = CGRect(x: UIScreen.main.bounds.width, y: 0, width: self.childWidth, height: UIScreen.main.bounds.height)
        } completion: { flag in
            if flag {
                completion?()
            }
        }
    }
    
    
}
